<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>虚拟库存同步记录 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <h3>虚拟库存同步记录</h3>
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="input-group">
                    <select id="supplierFilter" class="form-select">
                        <option value="">所有供应商</option>
                    </select>
                    <input type="text" id="productIdFilter" class="form-control" placeholder="商品ID">
                    <button id="searchBtn" class="btn btn-primary">查询</button>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>同步时间</th>
                            <th>供应商</th>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>同步前数量</th>
                            <th>同步后数量</th>
                            <th>操作人</th>
                        </tr>
                    </thead>
                    <tbody id="syncLogsTable"></tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 加载供应商列表（带/api前缀）
        const loadSuppliers = async () => {
            const res = await fetch('/api/supplier/list');
            const suppliers = await res.json();
            const selector = document.getElementById('supplierFilter');
            selector.innerHTML += suppliers.map(s => `<option value="${s.id}">${s.supplier_name}</option>`).join('');
        };

        // 加载同步记录（带/api前缀）
        const loadSyncLogs = async () => {
            const supplierId = document.getElementById('supplierFilter').value;
            const productId = document.getElementById('productIdFilter').value;
            const res = await fetch(`/api/supplier/sync/logs?supplier_id=${encodeURIComponent(supplierId)}&product_id=${encodeURIComponent(productId)}`);
            const logs = await res.json();
            const table = document.getElementById('syncLogsTable');
            table.innerHTML = logs.map(log => `
                <tr>
                    <td>${log.sync_time}</td>
                    <td>${log.supplier_name}</td>
                    <td>${log.product_id}</td>
                    <td>${log.product_name}</td>
                    <td>${log.old_quantity}</td>
                    <td>${log.new_quantity}</td>
                    <td>${log.operator}</td>
                </tr>
            `).join('');
        };

        document.getElementById('searchBtn').addEventListener('click', loadSyncLogs);
        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = async () => {
            await loadSuppliers();
            loadSyncLogs();
        };
    </script>
</body>
</html>